<?php 
class Common_Log
{
	const ERROR = "ERROR";
	const INFO = "INFO";
	const DEBUG = "DEBUG";
	const WARNING = "WARNING";
	const SESSION_ENABLE_KEY = "_LOG_ENABLE_";
	
	private static $session_key = "_COMMON_LOG_";
	private static $colors = array("ERROR"=>"red","INFO"=>"cyan","DEBUG"=>"orange","WARNING"=>"yellow");
	
	public static function log($message,$priority = self::DEBUG, $file = null, $line = null)
	{
		if( $_SESSION[self::SESSION_ENABLE_KEY] === true )
        if($file && $line)
			$_SESSION[self::$session_key][] = "<span style='color:".self::$colors[$priority].";'>[$priority]</span> - ".date("Y-m-d H:i:s")."<br />File:$file - Line: $line<br /><span style='color:white'>$message</span><br /><br />";
        else
            $_SESSION[self::$session_key][] = "<span style='color:".self::$colors[$priority].";'>[$priority]</span> - ".date("Y-m-d H:i:s")."<br /><span style='color:white'>$message</span><br /><br />";
	}
	
	public static function varDump($message,$priority = self::DEBUG)
	{
		if( $_SESSION[self::SESSION_ENABLE_KEY] === true )
			self::log( "<pre>".print_r($message,true)."</pre>",$priority);
	}
	
	public static function clear()
	{
		unset($_SESSION[self::$session_key]);
	}
	
	public static function enable($boolean = true,$history=false)
	{
		if(!$history) 
			self::clear();
		
		$_SESSION[self::SESSION_ENABLE_KEY] = $boolean;
	}
	
	public static function getLogs()
	{
		$logs = "";
		
		for($i = count($_SESSION[self::$session_key])-1; $i >= 0; $i--)
			$logs .= $_SESSION[self::$session_key][$i];
			
		return $logs;
	}
	
	public static function getHtmlHead()
	{
		if($_SESSION[self::SESSION_ENABLE_KEY] )
			return '<link href="/js/plugins/debug/slide.css" rel="stylesheet" type="text/css" />
			<script language="javascript" src="/js/plugins/debug/slide.js"></script>
			<link rel="stylesheet" type="text/css" media="all" href="/js/plugins/debug/jScrollPane.css" />
			<script type="text/javascript" src="/js/plugins/debug/jquery.mousewheel.js"></script>
			<script type="text/javascript" src="/js/plugins/debug/jScrollPane.js"></script>
			<script type="text/javascript" src="/js/plugins/debug/jquery.hotkeys.js"></script>
			<script type="text/javascript">
				var debugdesplegado = false;
				jQuery(document).bind("keydown", "f9",function (evt){
					if(debugdesplegado) 
					{
						$("div#panel").slideUp("slow"); 
						 debugdesplegado = false;
						 return false;
					}
					else
					{
						$("div#panel").slideDown("slow"); 
						debugdesplegado = true;
						return false;
					}
					
				});
			</script>
			';
			
		return "";
	}
	
	public static function getHtmlBody()
	{
		if($_SESSION[self::SESSION_ENABLE_KEY] )
						return '<!-- Panel -->
				<div id="toppanel">
					<div id="panel">
						<div class="content clearfix">
							<h1>PHP Console Debugger!</h1>
							<div style="height:200px; overflow: auto; padding: 5px 15px;">	
							<code>
									 '.self::getLogs().'
							</code>
							</div>	
						</div>
					</div> <!-- /login -->	
				    <!-- The tab on top -->	
					<div class="tab">
						<!--<ul class="login">
					    	<li class="left">&nbsp;</li>
					        <li>Consola</li>
							<li class="sep">|</li>
							<li id="toggle">
								<a id="open" class="open" href="#">Open</a>
								<a id="close" style="display: none;" class="close" href="#">Close</a>			
							</li>
							<li class="sep">|</li>
							<li>
								<a href="#" onclick="javascript:$(\'#toppanel\').remove()">X</a>
							</li>
					    	<li class="right">&nbsp;</li>
						</ul>--> 
					</div> <!-- / top -->
					
				</div> <!--panel -->
						';
						
		return "";
	}
}
?>
